﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>1.0.User表的增删改</title>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shCore.js"></script>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shBrushCSharp.js"></script>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shBrushXml.js"></script>
    <link type="text/css" rel="stylesheet" href="../../Content/highlighter/styles/shCore.css" />
    <link type="text/css" rel="stylesheet" href="../../Content/highlighter/styles/shCoreEclipse.css" />
    <script type="text/javascript">SyntaxHighlighter.all();</script>
</head>
<body>
    <h3>使用Render函数.</h3>
    <b>
        1.如何使用cshtml?
    </b>
    <pre class="brush: html;">
        //在项目Views/Index/下新建Index.cshtml
        //该视图继承自NFinal.Views.RazorView
        //其泛型参数为控制器自动生成的视图数据类
        @inherits NFinal.Views.RazorView&lt;NFinalCorePlug.Controllers.IndexController_Model.Index>
        &lt;!DOCTYPE html>
        &lt;html>
            &lt;head>&lt;/head>
            &lt;body>
                //在该视图中可以使用Razor的基础语法，@,if,while,for,foreach等。
                @Model.a
            &lt;/body>
        &lt;/html>
    </pre>
    <p>
        2.当保存以上文件时,Visual Studio会自动生成Index.template.cs类：
    </p>
    <pre class="brush: csharp">
        using System;
        using System.IO;
        using System.Net;
        using System.Collections.Generic;
        using NFinal;

        //此代码由NFinalRazorGenerator生成。
        //http://bbs.nfinal.com
        namespace NFinalCorePlug.Views.Index
        {
            //此属性参数为视图路径
            [View("/NFinalCorePlug/Views/Index/Default.cshtml")]
            public class Default : NFinal.View.RazorView&lt;NFinalCorePlug.Controllers.IndexController_Model.Index>
            {
                public Default(NFinal.IO.Writer writer, NFinalCorePlug.Controllers.IndexController_Model.Index Model) : base(writer, Model)
                {
                }
                //如果此处报错，请添加NFinal引用
                //PMC命令为：Install-Package NFinal
                public override void Execute()
                {
                    writer.Write("");
                    writer.Write("&lt;!DOCTYPE html>\r\n&lt;html>\r\n\r\n&lt;head>&lt;/head>\r\n&lt;body>");
                    writer.Write(Model.a);
                    writer.Write("&lt;/body>\r\n&lt;/html>");
                }
            }
        }
    </pre>
    <p>
        3.把视图数据传到模板中:
    </p>
    <pre class="brush: csharp;">
        public class IndexController:BaseController
        {
            [ViewBagMember]//把该参数添加到视图数据中
		    [Newtonsoft.Json.JsonIgnore]//转Json时忽略该参数
		    public string imageServerUrl;
            public void Index()
            {
                this.ViewBag.a=1;
                this.ViewBag.b="Hello!";
                //把以上的视图路径作为Render函数的参数
                this.Render("/NFinalCorePlug/Views/Index/Default.cshtml");
            }
        }
    </pre>
</body>
</html>